<?php

include dirname(dirname(dirname(__FILE__))) . '/include/common.php';

class webServiceUpdateGuest extends core {

    public function process() {
        $data = array();
        $guest_id = intval($_POST['guest_id']);
        $user_id = intval($_POST['user_id']);
        $data['firstname'] = strip_tags(trim($_POST['firstName']));
        $data['lastname'] = strip_tags(trim($_POST['lastName']));
        $data['email'] = $this->_cleanEmail($_POST['email']);
        $data['phone'] = $_POST['phone'];
        $data['address'] = strip_tags(trim($_POST['streetAddress']));
        $data['city'] = strip_tags(trim($_POST['city']));
        $data['state'] = strip_tags(trim($_POST['state']));
        $data['zipcode'] = strip_tags(trim($_POST['zipcode']));
        $data['user_id'] = $user_id;
        /* If userEmail is NOT found in the database => return error code 4 */
        $fields = array();
        $values = array();
        $this->_preDataForInsert($data, $fields, $values);
        $fields = implode(',', $fields);
        $values = implode(',', $values);
        if ($guest_id > 0) {
            $guest_id = $this->db->escape($guest_id);
            $data = $this->db->get_row("SELECT * FROM guests WHERE guest_id=$guest_id AND user_id=$user_id");
            if ($data) {
                $firstName = $this->db->escape($_POST['firstName']);
                $lastname = $this->db->escape($_POST['lastName']);
                $email = $this->_cleanEmail($_POST['email']);
                $phone = $this->db->escape($_POST['phone']);
                $address = $this->db->escape($_POST['streetAddress']);
                $city = $this->db->escape($_POST['city']);
                $state = $this->db->escape($_POST['state']);
                $zipcode = $this->db->escape($_POST['zipcode']);
                $query ="UPDATE `guests` SET `firstName`='$firstName',`lastname`='$lastname',`email`='$email',`phone`= '$phone',`address`='$address',
                        `city`='$city',`state`='$state',`zipcode`='$zipcode' WHERE `guest_id`= '$guest_id'";
                
                if ($this->db->query($query)===false) {

                    $this->_JSONResponse(array('error' => 1, 'msg' => 'Contact information has not been edited, please save again'));
                } else {
                    $this->_JSONResponse(array('error' => 0, 'msg' => 'Contact information has been saved'));
                }
            } else {
                $this->_JSONResponse(array('error' => 1, 'msg' => 'Contact does not exist'));
            }
        } else {
            if ($this->db->query("INSERT INTO `guests` ({$fields}) VALUES ({$values});")===false) {

                $this->_JSONResponse(array('error' => 1, 'msg' => 'Oops, something went wrong. Please try again'));
            } else {
                $this->_JSONResponse(array('error' => 0, 'msg' => 'A contact has been added.'));
            }
        }
    }

}

$processor = new webServiceUpdateGuest();
$processor->process();
?>